
package com.hlkj.pay.app.merchant.ext.impl.easypay.handler;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.id.IdUtils;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantAccessContext;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantAccessQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantAccessSignContext;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantBusinessContext;
import com.hlkj.pay.app.merchant.ext.impl.easypay.constant.EasyPayConstant;
import com.hlkj.pay.app.merchant.ext.impl.easypay.dto.EasyPayConfig;
import com.hlkj.pay.app.merchant.ext.impl.easypay.utils.EasyPaySignUtil;
import com.hlkj.pay.app.merchant.ext.impl.easypay.utils.HttpUtils;
import com.hlkj.pay.dto.merchant.MerchantAccessSignLink;
import com.hlkj.pay.dto.order.PayOrderFundDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelRequestDO;
import com.hlkj.pay.util.DateUtils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/16 9:10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EasyPayMerchantAccessInfoHandler extends EasyPayMerchantCommonParamsHandle {

    @Value("${hlkj.domain}")
    private String domain;

    /**
     * @param channelMerchantAccessContext
     */
    public void merchantAccess(ChannelMerchantAccessContext channelMerchantAccessContext) {
        log.info("易生渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessContext));
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantAccessContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessContext.setRemark(easyPayConfigCommonResult.getMsg());
            channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        // 1. 数据初始化
        PayChannelRequestDO payChannelRequestDO = new PayChannelRequestDO();
        channelMerchantAccessContext.setPayChannelRequestDO(payChannelRequestDO);
        try {
            // 参数组装
            // 新增参数转换
            String requestId = IdUtils.getSnowflake().nextIdStr();
            channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessInfo().setRequestId(requestId);
            CommonResult<Map<String, String>> paramsCommonResult = EasyPayMerchantParamsHandle.merchantApplyParams(channelMerchantAccessContext, easyPayConfig, domain);
            if (!paramsCommonResult.isSuccess()) {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                channelMerchantAccessContext.setRemark(paramsCommonResult.getMsg());
                log.error("易生开户参数校验失败 message: {}", paramsCommonResult.getMsg());
                return;
            }
            Map<String, String> params = paramsCommonResult.getData();
            params.put("MAC", EasyPaySignUtil.md5Sign(params, easyPayConfig.getMd5Key()));
            // 记录请求参数
            payChannelRequestDO.setRequestId(requestId);
            payChannelRequestDO.setRequestParams(JsonUtils.toJsonString(params));
            String url = domain(channelMerchantAccessContext.isTest());
            payChannelRequestDO.setRequestTime(System.currentTimeMillis());

            log.info("易生新增进件请求参数:{}", JsonUtils.toJsonString(params));
            String response = HttpUtils.requestPost(url, params);
            log.info("易生新增进件返回参数:{}", response);
            payChannelRequestDO.setResponse(response);
            JSONObject obj = JsonUtils.parseObj(response);
            EasyPayMerchantParamsHandle.merchantApplyParamsParse(obj, channelMerchantAccessContext);
        } catch (Exception e) {
            log.error("易生开户请求失败,失败信息 message: {}", e.getMessage(), e);
            channelMerchantAccessContext.setRemark("易生开户请求失败,失败信息" + (StringUtils.hasText(e.getMessage()) ? e.getMessage() : " 数据解析异常"));
            channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 创建电子协议
     *
     * @param channelMerchantAccessSignContext
     */
    public void processElecContract(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantAccessSignContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessSignContext.setRemark(easyPayConfigCommonResult.getMsg());
            channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        // 1. 电子协议开户
        PayChannelRequestDO payChannelRequestDO = new PayChannelRequestDO();
        channelMerchantAccessSignContext.setPayChannelRequestDO(payChannelRequestDO);
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessSignContext.getMerchantAccessInfoDO();
        try {
            CommonResult<Map<String, String>> paramsCommonResult = EasyPayMerchantParamsHandle.contractApply(merchantAccessInfo.getApplyNo(), easyPayConfig);
            Map<String, String> params = paramsCommonResult.getData();
            params.put("MAC", EasyPaySignUtil.md5Sign(params, easyPayConfig.getMd5Key()));
            String url = domain(channelMerchantAccessSignContext.isTest());
            log.info("易生签署电子协议请求参数:{}", JsonUtils.toJsonString(params));
            String response = HttpUtils.requestPost(url, params);
            log.info("易生签署电子协议返回参数:{}", response);
            JSONObject elecContract = JsonUtils.parseObj(response);
            if (!"0000".equals(elecContract.getStr("retCode"))) {
                channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                channelMerchantAccessSignContext.setRemark(elecContract.getStr("retMsg"));
                log.error("易生签署电子协议失败 message: {}", elecContract.getStr("retMsg"));
                return;
            }
            merchantAccessInfo.setStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING.getCode());
            channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
            MerchantAccessSignLink merchantAccessSignLink = new MerchantAccessSignLink();
            channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SIGN);
            merchantAccessSignLink.setLink(elecContract.getStr("contractUrl"));
            merchantAccessSignLink.setName("易生电子协议");
            channelMerchantAccessSignContext.setSignLinks(new ArrayList<>() {
                {
                    add(merchantAccessSignLink);
                }
            });
        } catch (Exception e) {
            log.error("易生创建协议请求失败,失败信息 message: {}", e.getMessage(), e);
            channelMerchantAccessSignContext.setRemark("易生创建协议请求失败,失败信息" + e.getMessage());
            channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
        }
    }

    /**
     * @param channelMerchantBusinessContext
     */
    public void auditProcess(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantBusinessContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(easyPayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        String requestUrl = EasyPayConstant.ELECTRONIC_CONTRACT_SIGN;
//        CommonResult<Map<String, Object>> paramsCommonResult  = EasyPayMerchantParamsHandle.(channelMerchantBusinessContext, easyPayConfig);

    }

    /**
     * 进件查询
     *
     * @param channelMerchantAccessQueryContext
     */
    public void accessQueryRequest(ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantAccessQueryContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessQueryContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessQueryContext.setRemark(easyPayConfigCommonResult.getMsg());
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        try {
            CommonResult<Map<String, String>> paramsCommonResult  = EasyPayMerchantParamsHandle.queryApplyStatus(channelMerchantAccessQueryContext.getMerchantAccessInfo().getRequestId(), easyPayConfig);
            Map<String, String> params = paramsCommonResult.getData();
            params.put("MAC", EasyPaySignUtil.md5Sign(params, easyPayConfig.getMd5Key()));
            String url = domain(channelMerchantAccessQueryContext.isTest());
            log.info("易生进件查询请求参数:{}", JsonUtils.toJsonString(params));
            String response = HttpUtils.requestPost(url, params);
            log.info("易生进件查询返回参数:{}", response);
            JSONObject respObj = JsonUtils.parseObj(response);
            if (!"0000".equals(respObj.getStr("retCode"))) {
                channelMerchantAccessQueryContext.setRemark(respObj.getStr("retMsg"));
                log.error("易生进件查询异常 message: {}", respObj.getStr("retMsg"));
                channelMerchantAccessQueryContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                channelMerchantAccessQueryContext.setRemark(respObj.getStr("retMsg"));
                return;
            }
            //9-待处理；3-待审核；0-审核通过；X-审核失败；1-审核驳回；5-待签约
             switch (respObj.getStr("auditStatus")) {
                case "0":
                    //成功
//                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                    break;
                case "5":
                    //成功
                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                    break;
                case "X":
                case "1":
                    //失败
                    channelMerchantAccessQueryContext.setProcessStatus(Boolean.FALSE);
                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    break;
                default:
                    //审核中
//                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                    break;
            }
            if (respObj.containsKey("auditMsg"))
                channelMerchantAccessQueryContext.setRemark(respObj.getStr("auditMsg"));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("易生进件查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessQueryContext.setRemark("易生进件查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessQueryContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
        }
    }

    /**
     * 进件查询2
     *
     * @param channelMerchantAccessSignContext
     */
    public void accessQueryRequest(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantAccessSignContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessSignContext.setRemark(easyPayConfigCommonResult.getMsg());
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        try {
            MerchantAccessInfoDO merchantAccessInfoDO = channelMerchantAccessSignContext.getMerchantAccessInfoDO();
            CommonResult<Map<String, String>> paramsCommonResult  = EasyPayMerchantParamsHandle.queryApplyStatus(merchantAccessInfoDO.getRequestId(), easyPayConfig);
            Map<String, String> params = paramsCommonResult.getData();
            params.put("MAC", EasyPaySignUtil.md5Sign(params, easyPayConfig.getMd5Key()));
            String url = domain(channelMerchantAccessSignContext.isTest());
            log.info("易生签约后进件查询请求参数:{}", JsonUtils.toJsonString(params));
            String response = HttpUtils.requestPost(url, params);
            log.info("易生签约后进件查询返回参数:{}", response);
            JSONObject respObj = JsonUtils.parseObj(response);
            if (!"0000".equals(respObj.getStr("retCode"))) {
                channelMerchantAccessSignContext.setRemark(respObj.getStr("retMsg"));
                log.error("易生进件查询异常 message: {}", respObj.getStr("retMsg"));
                channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                channelMerchantAccessSignContext.setRemark(respObj.getStr("retMsg"));
                return;
            }
            //9-待处理；3-待审核；0-审核通过；X-审核失败；1-审核驳回；5-待签约
            channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
            switch (respObj.getStr("auditStatus")) {
                case "0":
                    //成功
                    JSONObject sysInfo = respObj.getJSONArray("sysInfo").getJSONObject(0);
                    merchantAccessInfoDO.setSubMchId(sysInfo.getStr("merCode"));
                    merchantAccessInfoDO.setExtendA(respObj.getStr("sysInfo"));
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
                    channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMERINFO);
                    break;
                case "5":
                    //成功
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
                    break;
                case "3":
                    //签约后待审核
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
                    channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
                    break;
                case "X":
                case "1":
                    //失败
                    channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    break;
                default:
                    //审核中
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
                    break;
            }
            if (respObj.containsKey("auditMsg"))
                channelMerchantAccessSignContext.setRemark(respObj.getStr("auditMsg"));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("易生进件查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessSignContext.setRemark("易生进件查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
        }
    }
    /**
     * 进件查询 配置时查询签约状态
     *
     * @param channelMerchantBusinessContext
     */
    public void accessQueryRequest(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantBusinessContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            channelMerchantBusinessContext.setRemark(easyPayConfigCommonResult.getMsg());
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        try {
            MerchantAccessInfoDO merchantAccessInfoDO = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
            CommonResult<Map<String, String>> paramsCommonResult  = EasyPayMerchantParamsHandle.queryApplyStatus(merchantAccessInfoDO.getRequestId(), easyPayConfig);
            Map<String, String> params = paramsCommonResult.getData();
            params.put("MAC", EasyPaySignUtil.md5Sign(params, easyPayConfig.getMd5Key()));
            String url = domain(channelMerchantBusinessContext.isTest());
            log.info("易生配置进件查询请求参数:{}", JsonUtils.toJsonString(params));
            String response = HttpUtils.requestPost(url, params);
            log.info("易生配置进件查询返回参数:{}", response);
            JSONObject respObj = JsonUtils.parseObj(response);
            if (!"0000".equals(respObj.getStr("retCode"))) {
                channelMerchantBusinessContext.setRemark(respObj.getStr("retMsg"));
                log.error("易生进件查询异常 message: {}", respObj.getStr("retMsg"));
                channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                channelMerchantBusinessContext.setRemark(respObj.getStr("retMsg"));
                return;
            }
            //9-待处理；3-待审核；0-审核通过；X-审核失败；1-审核驳回；5-待签约
            channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
            switch (respObj.getStr("auditStatus")) {
                case "0":
                    //成功
                    JSONObject sysInfo = respObj.getJSONArray("sysInfo").getJSONObject(0);
                    merchantAccessInfoDO.setSubMchId(sysInfo.getStr("merCode"));
                    merchantAccessInfoDO.setExtendA(respObj.getStr("sysInfo"));
                    channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
                    channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMERINFO);
                    break;
                case "5":
                    //成功
                    channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
                    break;
                case "3":
                    //签约后待审核
                    channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
                    channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
                    break;
                case "X":
                case "1":
                    //失败
                    channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
                    channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    break;
                default:
                    //审核中
                    channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
                    break;
            }
            if (respObj.containsKey("auditMsg"))
                channelMerchantBusinessContext.setRemark(respObj.getStr("auditMsg"));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("易生进件查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("易生进件查询请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
        }
    }

    /**
     * 商户余额查询
     * 实现暂不使用 没有测试  有问题
     * @param channelMerchantBusinessContext
     */
    public void queryBalance(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantBusinessContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(easyPayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        String requestUrl = domain(channelMerchantBusinessContext.isTest()) + EasyPayConstant.QUERY_BALANCE;

        try {
            MerchantAccessInfoDO merchantAccessInfoDO = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
            Map<String, Object> reqBody = new HashMap<>();
            Map<String, String> data = new HashMap<>();
            data.put("macCode", "000000");
            data.put("merCode", merchantAccessInfoDO.getSubMchId());
            data.put("orgCode", easyPayConfig.getReqId());
            data.put("sedTradeDate", DateUtils.getDateString(DatePattern.PURE_DATE_PATTERN));
            data.put("tradeOrder", IdUtils.getSnowflake().nextIdStr());
            reqBody.put("data", data);
            reqBody.put("orgId", easyPayConfig.getReqId());
            reqBody.put("orgMercode", merchantAccessInfoDO.getSubMchId());

//            MerchantAppChannelDO merchantAppChannelDO = channelMerchantBusinessContext.getMerchantAppChannelDO();
//            EasypayAppChannelParams easypayAppChannelParams = easyPayAppChannelParams(merchantAppChannelDO);
//
//            reqBody.put("orgTermno", merchantAccessInfoDO.getExtendA());
            reqBody.put("signType", "RSA2");
            reqBody.put("sign", EasyPaySignUtil.sign10(data, easyPayConfig.getRsaPrivateKey()));

            log.info("易生结算记录查询req参数:{}", JsonUtils.toJsonString(reqBody));
            String response = HttpUtils.post(requestUrl, reqBody);
            log.info("易生结算记录查询返回参数:{}", response);
            JSONObject respObj = JsonUtils.parseObj(response);
            if (!"000000".equals(respObj.getStr("sysRetcode"))) {
                channelMerchantBusinessContext.setRemark(respObj.getStr("sysRetmsg"));
                log.error("易生结算记录查询异常 message: {}", respObj.getStr("sysRetmsg"));
                return;
            }
            if (!"00".equals(respObj.getStr("resultCode"))) {
                channelMerchantBusinessContext.setRemark(respObj.getStr("resultMsg"));
                log.error("易生结算记录查询失败 message: {}", respObj.getStr("resultMsg"));
                return;
            }
            //余额
            JSONObject balanceObj = respObj.getJSONObject("data");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("易生结算记录查询请求失败 message:{}", e.getMessage(), e);
            channelMerchantBusinessContext.setRemark("易生结算记录查询请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
        }

    }

    /**
     *
     * 实现暂不使用 没有测试  有问题
     * @param channelMerchantBusinessContext
     */
    public void querySettleInfo(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<EasyPayConfig> easyPayConfigCommonResult = channelConfig(channelMerchantBusinessContext.getProviderChannelConfig());
        // 配置转换
        if (!easyPayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(easyPayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        EasyPayConfig easyPayConfig = easyPayConfigCommonResult.getData();
        String requestUrl = domain(channelMerchantBusinessContext.isTest()) + EasyPayConstant.QUERY_SETTLE_INFO;

        try {
            MerchantAccessInfoDO merchantAccessInfoDO = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
//            channelMerchantBusinessContext.getMerchantAccessDto()
            Map<String, Object> reqBody = new HashMap<>();

            Map<String, String> data = new HashMap<>();
            //结算日期
            data.put("stlmDate", DateUtils.getDateString(DatePattern.PURE_DATE_PATTERN));
            reqBody.put("data", data);
            reqBody.put("orgId", easyPayConfig.getReqId());
            reqBody.put("orgMercode", merchantAccessInfoDO.getSubMchId());
//            reqBody.put("orgTermno", merchantAccessInfoDO.getExtendA());
            reqBody.put("signType", "RSA2");
            reqBody.put("sign", EasyPaySignUtil.sign10(data, easyPayConfig.getRsaPrivateKey()));

            log.info("易生结算记录查询req参数:{}", JsonUtils.toJsonString(reqBody));
            String response = HttpUtils.post(requestUrl, reqBody);
            log.info("易生结算记录查询返回参数:{}", response);
            JSONObject respObj = JsonUtils.parseObj(response);
            if (!"000000".equals(respObj.getStr("sysRetcode"))) {
                channelMerchantBusinessContext.setRemark(respObj.getStr("sysRetmsg"));
                log.error("易生结算记录查询异常 message: {}", respObj.getStr("sysRetmsg"));
                return;
            }
            if (!"00".equals(respObj.getStr("resultCode"))) {
                channelMerchantBusinessContext.setRemark(respObj.getStr("resultMsg"));
                log.error("易生结算记录查询失败 message: {}", respObj.getStr("resultMsg"));
                return;
            }
            //记录列表
            JSONArray dataList = respObj.getJSONArray("data");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("易生结算记录查询请求失败 message:{}", e.getMessage(), e);
            channelMerchantBusinessContext.setRemark("易生结算记录查询请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
        }

    }

    public BigDecimal sum(List<PayOrderFundDto> payOrderFundDtos, boolean orderAmount){
        BigDecimal amount = new BigDecimal(0);
        for(PayOrderFundDto payOrderFundDto:payOrderFundDtos){
            if(orderAmount){
                amount = amount.add(new BigDecimal(StrUtil.isEmpty(payOrderFundDto.getAmount())?"0":payOrderFundDto.getAmount()));
            }else{
                amount = amount.add(new BigDecimal(StrUtil.isEmpty(payOrderFundDto.getSepaFeeRatio())?"0":payOrderFundDto.getSepaFeeRatio()));
            }
        }
        return amount;
    }


}
